package com.ewanghuiju.app.ui.my.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.my.MyInfoSettingContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.presenter.my.MyInfoSettingPresenter;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.EditTextWithDel;
import com.gyf.immersionbar.ImmersionBar;
import com.muddzdev.styleabletoast.StyleableToast;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 我的信息设置页面
 * @time 2020/2/24
 */
public class MyInfoSettingActivity extends BaseActivity<MyInfoSettingPresenter> implements MyInfoSettingContract.View {

    @BindView(R.id.tv_tag_text)
    TextView tvTagText;
    @BindView(R.id.et_input)
    EditTextWithDel etInput;
    @BindView(R.id.tv_lab)
    TextView tvLab;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    private int mType;

    @Override
    protected int getLayout() {
        return R.layout.activity_my_info_setting;
    }

    @Override
    protected void initEventAndData() {
        mType = getIntent().getIntExtra(Constants.SETTING_TYPE, 1);
        if (mType == 1) {
            tvTitle.setText("设置微信号");
            tvTagText.setText(getString(R.string.setting_wx_no_lab));
            etInput.setHint(getString(R.string.setting_please_input_wxno));
            tvLab.setVisibility(View.VISIBLE);
        } else if (mType == 2) {
            tvTitle.setText(R.string.setting_nickname_set);
            tvTagText.setText(getString(R.string.setting_nickname_lab));
            etInput.setHint(getString(R.string.setting_please_input_nickname));
            tvLab.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.tv_submit})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_submit:
                //提交
                String inputText = etInput.getText().toString();
                if (mType == 1) {
                    //设置微信
                    if (TextUtils.isEmpty(inputText)) {
                        StyleableToast.makeText(mContext, getString(R.string.setting_please_input_wxno), Toast.LENGTH_SHORT, R.style.mytoast).show();
                        return;
                    }
                    LoadingDialogUtils.show(mContext);
                    mPresenter.settingWxCard(inputText);
                } else if (mType == 2) {
                    //设置昵称
                    if (TextUtils.isEmpty(inputText)) {
                        StyleableToast.makeText(mContext, getString(R.string.setting_please_input_nickname), Toast.LENGTH_SHORT, R.style.mytoast).show();
                        return;
                    }
                    LoadingDialogUtils.show(mContext);
                    mPresenter.settingNickname(inputText);
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void settingWxCardSuccess() {
        LoadingDialogUtils.dismissDialog_ios();
        StyleableToast.makeText(mContext, "设置成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
        //刷新用户信息
        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_USERINFO));
        onBackPressedSupport();
    }

    @Override
    public void settingNicknameSuccess() {
        //设置昵称成功
        LoadingDialogUtils.dismissDialog_ios();
        StyleableToast.makeText(mContext, "设置成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
        //刷新用户信息
        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_USERINFO));
        onBackPressedSupport();
    }
}
